﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.DirectoryServices;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;

namespace IT_Pro.Forms
{
    public partial class frmComputerManager : DevExpress.XtraEditors.XtraForm
    {
        public frmComputerManager()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
        }

        
        private List<string> GetComputersOnNetwork()
        {
           // int i = 0;
            List<string> list = new List<string>();
            using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
            {
                foreach (DirectoryEntry computers in root.Children)
                {
                
                  //  progressBar1.Maximum=root.Children.

                    foreach (DirectoryEntry computer in computers.Children)
                    {
                       // == "Computer"
                       if ((computer.Name != "Schema"))
                       
                        {
                            if (computer.SchemaClassName == "Computer")
                            {
                                AddToListBox(computer.Name + " ======> Checking Online...");
                                try
                                {
                                    Ping Pingsender = new Ping();
                                    string data = "aaaaaabbbaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
                                    byte[] buffer = Encoding.ASCII.GetBytes(data);
                                    int timeout = 540;
                                    PingReply reply = Pingsender.Send(computer.Name, timeout, buffer);

                                    if (reply.Status == IPStatus.Success)
                                    {
                                        list.Add(computer.Name);
                                        EditToListBox(computer.Name + " ======>Online", listComputer.Items.Count - 1);
                                    }
                                    else
                                    {
                                        RemoveToListBox(listComputer.Items.Count - 1);
                                    }
                                }
                                catch { RemoveToListBox(listComputer.Items.Count - 1); }

                                continue;
                            }
                           // backgroundWorker1.ReportProgress(i);
                        }
                    }
                }
            }
            return list;
        }

       
        private void btnLoadList_Click(object sender, EventArgs e)
        {
            progressBar1.Visible = true;
            backgroundWorker1.RunWorkerAsync();
        }
             
        List<string> lC = new List<string>();
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            lC = GetComputersOnNetwork();                 
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {            
            progressBar1.Visible = false;
        }

        private void btnCheckPolicy_Click(object sender, EventArgs e)
        {
            progressBar1.Visible = true;    
            backgroundWorker2.RunWorkerAsync();            
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {           
            if (lC.Count > 0)
            {
                for (int i = 0; i < lC.Count; i++)
                {
                    EditToListBox(lC[i].ToString() + " ======> Checking....", i);
                    string link = @"\\" + lC[i].ToString() + @"\c$\\WINDOWS\ClientPolicy.exe";
                   
                    if (File.Exists(link))
                    {
                        EditToListBox(lC[i].ToString()+ "=======> OK",i);
                    }
                    else
                    {
                        EditToListBox(lC[i].ToString()+ "=======> Bad",i);
                    }                   
                }
            }
        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {          
            progressBar1.Visible = false;
        }

        private void AddToListBox(object oo)
        {
            Invoke(new MethodInvoker(
                           delegate { listComputer.Items.Add(oo); }
                           ));
        }
        private void EditToListBox(object oo,int Position)
        {
            Invoke(new MethodInvoker(
                           delegate { listComputer.Items[Position] = oo; }
                           ));
        }
        private void ClearToListBox()
        {
            Invoke(new MethodInvoker(
                           delegate { listComputer.Items.Clear(); }
                           ));
        }

        private void RemoveToListBox(int Positon)
        {
            Invoke(new MethodInvoker(
                           delegate { listComputer.Items.RemoveAt(Positon); }
                           ));
        }

        private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
        {
            if (lC.Count > 0)
            {
                for (int i = 0; i < lC.Count; i++)
                {
                    EditToListBox(lC[i].ToString() + " ======> Copying....", i);
                    try
                    {
                      //  File.Copy(@"\\192.168.1.15\Software\ClientPolicy.exe", @"\\" + "it-nttoan" + @"\c$\\WINDOWS\ClientPolicy.exe", true);
                        File.Copy(@"\\192.168.1.15\Software\ClientPolicy.exe", @"\\" + lC[i].ToString() + @"\c$\\WINDOWS\ClientPolicy.exe",true);
                        if (Directory.Exists(@"\\" + lC[i].ToString() + @"\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"))
                        {
                            File.Copy(@"\\192.168.1.15\Software\ClientPolicy.lnk", @"\\" + lC[i].ToString() + @"\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\ClientPolicy.lnk", true);
                        }
                        else if (Directory.Exists(@"\\" + lC[i].ToString() + @"\c$\Users\All Users\Microsoft\Windows\Start Menu\Programs\Startup"))
                        {
                            File.Copy(@"\\192.168.1.15\Software\ClientPolicy.lnk", @"\\" + lC[i].ToString() + @"\c$\Users\All Users\Microsoft\Windows\Start Menu\Programs\Startup\ClientPolicy.lnk", true);
                        }
                        else
                        {
                            File.Copy(@"\\192.168.1.15\Software\ClientPolicy.lnk", @"\\" + lC[i].ToString() + @"\c$\Documents and Settings\All Users\Start Menu\Programs\Startup\ClientPolicy.lnk", true);
                        }
                        //"\\192.168.1.15\Software\ClientPolicy.exe"
                        // \\192.168.1.15\Software\ClientPolicy.lnk
                        //"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"

                        EditToListBox(lC[i].ToString() + " ======> Copy Success...", i);

                    }
                    catch { EditToListBox(lC[i].ToString() + " ======> Copy Fail...", i); }

                    
                }
            }
        }

        private void backgroundWorker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Visible = false;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            progressBar1.Visible = true;
            backgroundWorker3.RunWorkerAsync();  
        }

        private void listComputer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int t=listComputer.SelectedIndex;
                listComputer.Items.RemoveAt(t);
                lC.RemoveAt(t);
            }
        }
    }
}